/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.view;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

/**
 *
 * @author chenzhuohua
 */
public class ItscLoginCheckListener implements PhaseListener {

    public void afterPhase(PhaseEvent event) {
        
        // 检查是否是登录页面.
        FacesContext fc = event.getFacesContext();
        boolean loginPage =
	  fc.getViewRoot().getViewId().lastIndexOf("template-client") > -1 ? true : false;

        // 非登录界面且用户未登录，转向登录界面
        if (!loginPage && !loggedIn()) {
            NavigationHandler nh = fc.getApplication().getNavigationHandler();
            nh.handleNavigation(fc, null, "user_login");
        }
    }

    public void beforePhase(PhaseEvent event) {
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }

    private boolean loggedIn() {
        UserloginBean loginBean = (UserloginBean) FacesUtil.getManagedBean("userLoginBean");
        return loginBean.isLogin();
    }
}
